import MSData
import WorkerMSDB
import MSDBTemplates
import MSConfig
import MSLogger

import BaseHTTPServer
import time
import os
import sys
import pdb



MSLogger.logInit('C:\\Soft\\ms_logs\\ms_webui.log', 6, True)
reader = WorkerMSDB.Worker('localhost', '1433', 'base', 'mssa', 'msms')
reader.connect()
mobile_groups = []
MSDBTemplates.GET_AllObjects_FROM_CarOnline(reader.connection.cursor(), mobile_groups)

#p - page
def writeBegin(p):
    p.write('\
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\
        <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">\
          <head>\
            <meta http-equiv="content-type" content="text/html; charset=utf-8"/>\
            <title>Google Maps JavaScript API Example: Simple Icon</title>\
            <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAqOj1h9QN91TQBa2M4tBMcxT2yXp_ZAY8_ufC3CFXhHIE1NvwkxRc2tmSQwmGIxTGxtZIgE5dBXq5vw"\
                    type="text/javascript"></script>\
            <script type="text/javascript">\
    ')

def writeMobileGroup(p, mobile_groups):
    #blueIcon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/blank.png";\
    p.write('\
            function mapMobileGroups(map) {\
                var blueIcon = new GIcon(G_DEFAULT_ICON);\
                blueIcon.iconSize = new GSize(45,30);\
                blueIcon.image = "http://extras.inyork.com/redesign2010/images/autoIcon.png";\
                markerOptions = { icon:blueIcon };\
    ')
    for o in mobile_groups:
        if not o.eventsGPS: continue
        if len(o.eventsGPS) == 0: continue
        last = o.eventsGPS[len(o.eventsGPS) - 1]
        p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (last.GPSLatitudeOrig, last.GPSLongitudeOrig))

    p.write('\
            }\
    ')

def writeAnchors(p):
    p.write('\
            function mapAnchors(map) {\
                var anchorIcon = new GIcon(G_DEFAULT_ICON);\
                anchorIcon.iconSize = new GSize(30,30);\
                anchorIcon.shadowSize = new GSize(10, 10);\
                anchorIcon.image = "http://icons.iconarchive.com/icons/diveandgo/diving/256/anchor-icon.png";\
                markerOptions = { icon:anchorIcon };\
    ')
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.982309), str(30.252185)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.981407), str(30.273643)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.983275), str(30.298662)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.978229), str(30.318661)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.959264), str(30.335054)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.952711), str(30.354753)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.937128), str(30.292568)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.936870), str(30.301538)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.947553), str(30.305443)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.947360), str(30.333509)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.949896), str(30.363207)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.948993), str(30.400372)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.948693), str(30.405650)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.925775), str(30.391059)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.906371), str(30.423203)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.880345), str(30.448008)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.874897), str(30.459037)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.868585), str(30.483327)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.843349), str(30.494313)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.836988), str(30.518260)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.972195), str(29.788742)))
    p.write('map.addOverlay(new GMarker(new GLatLng(%s, %s), markerOptions));' % (str(59.924399), str(29.775696)))
    p.write('\
            }\
    ')


def writeEnd(p):
    p.write('\
            function initialize() {\
              if (GBrowserIsCompatible()) {\
                var map = new GMap2(document.getElementById("map_canvas"));\
                map.setCenter(new GLatLng(59.986379, 30.248780), 10);\
                map.setUIToDefault();\
                mapAnchors(map);\
                mapMobileGroups(map);\
                var trafficOptions = {incidents:true};\
                trafficInfo = new GTrafficOverlay(trafficOptions);\
                map.addOverlay(trafficInfo);\
              }\
            }\
            </script>\
          </head>\
          <body onload="initialize()" onunload="GUnload()">\
            <div id="map_canvas" style="width: 1000px; height: 600px"></div>\
          </body>\
        </html>\
    ')


class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        MSLogger.logInfo('process GET request')
        self.send_response(200)
        self.end_headers()

        reader.readEventsGPS(mobile_groups)

        writeBegin(self.wfile)
        writeMobileGroup(self.wfile, mobile_groups)
        writeAnchors(self.wfile)
        writeEnd(self.wfile)

#        reader.finalyze(mobile_groups)
        return


if __name__ == '__main__':
    from BaseHTTPServer import HTTPServer
    server = HTTPServer(('localhost', 8088), MyHandler)
    MSLogger.logInfo('ms local webui started, <Ctrl-C> to stop')
    server.serve_forever()



